﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace DIProject.Util.Discount
{
    /// <summary>
    /// общие действия для скидок
    /// </summary>
    public class Common
    {
        private const string _fileName = "/Discount.xml";
        private XmlDocument _doc = new XmlDocument();
        /// <summary>
        /// путь к xml файлу
        /// </summary>
        public string FilePath
        {
            get 
            {
                string curDirectory = Directory.GetCurrentDirectory();
                string fileName = curDirectory + _fileName;
                if (System.IO.File.Exists(fileName))
                    return fileName;
                return string.Empty;
            }           
        }
        public Common()
        { }
        private void Load()
        {
            try
            {
                if (string.IsNullOrEmpty(this.FilePath))
                    return;
                _doc.Load(this.FilePath);
            }
            catch (Exception ee)
            {
                System.Windows.Forms.MessageBox.Show("Не удалось загрузить конфигурационный файл скидок");
            }
        }
        private static DiscountCollection _discounts = null;
        public DiscountCollection Discounts
        {
            get
            {
                if (_discounts != null)
                    return _discounts;
                _discounts = new DiscountCollection();
                _discounts.Add(OneDiscount.Empty);
                Load();
                foreach (XmlNode node in _doc.SelectNodes("root/discount"))
                {
                    string description = node.Attributes["description"].Value;
                    string value = node.Attributes["value"].Value;
                    byte bValue;
                    if (!byte.TryParse(value, out bValue))
                    {
                        throw new Util.DIException("Не возможно конвертировать скидку в целое число, исправьте конфигурационный файл");
                    }
                    OneDiscount od = new OneDiscount();
                    od.Description = description;
                    od.Discount = bValue;
                    _discounts.Add(od);
                }
                return _discounts;
            }
        }

    }
    /// <summary>
    /// одна скидка
    /// </summary>
    public class OneDiscount
    {
        private string _description;
        /// <summary>
        /// Описание
        /// </summary>
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private byte _discount;
        /// <summary>
        /// Значение скидки
        /// </summary>
        public byte Discount
        {
            get 
            { 
                return _discount; 
            }
            set 
            {
                if (value > 100)
                    throw new Util.DIException("Скидка может быть от 0 до 100");
                _discount = value; 
            }
        }
        private static OneDiscount _empty;
        static OneDiscount()
        {
            _empty = new OneDiscount();
            _empty.Description = "Нет скидки";
            _empty.Discount = 0;
        }
        public static OneDiscount Empty
        {
            get
            {
                return _empty;
            }
        }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            if (!(obj is OneDiscount))
                return false;
            OneDiscount tmp = obj as OneDiscount;
            return ((tmp.Description == this.Description) && (tmp.Discount == this.Discount));
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return this.Description;
        }
    }
    public class DiscountCollection:IEnumerable<OneDiscount>,IList<OneDiscount>,System.Collections.IList
    {
        List<OneDiscount> _data = new List<OneDiscount>();
        #region IEnumerable<OneDiscount> Members

        public IEnumerator<OneDiscount> GetEnumerator()
        {
           return _data.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
           return _data.GetEnumerator();
        }

        #endregion

        #region IList<OneDiscount> Members

        public int IndexOf(OneDiscount item)
        {
            return _data.IndexOf(item);
        }

        public void Insert(int index, OneDiscount item)
        {
            _data.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _data.RemoveAt(index);
        }

        public OneDiscount this[int index]
        {
            get
            {                
                return _data[index];
            }
            set
            {
                _data[index] = value;
            }
        }

        #endregion

        #region ICollection<OneDiscount> Members

        public void Add(OneDiscount item)
        {
            _data.Add(item);
        }

        public void Clear()
        {
            _data.Clear();
        }

        public bool Contains(OneDiscount item)
        {
            return _data.Contains(item);
        }

        public void CopyTo(OneDiscount[] array, int arrayIndex)
        {
            _data.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _data.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(OneDiscount item)
        {
           return _data.Remove(item);
        }

        #endregion

        #region IList Members

        public int Add(object value)
        {
            _data.Add(value as OneDiscount);
            return 0;
        }

        public bool Contains(object value)
        {
            return _data.Contains(value as OneDiscount);
        }

        public int IndexOf(object value)
        {
            return _data.IndexOf(value as OneDiscount);
        }

        public void Insert(int index, object value)
        {
            _data.Insert(index, value as OneDiscount);
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            _data.Remove(value as OneDiscount);
        }

        object System.Collections.IList.this[int index]
        {
            get
            {
                return _data[index];
            }
            set
            {
                _data[index] = value as OneDiscount;
            }
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            throw new Exception("The method or operation is not implemented."); 
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }
}
